using System;
using System.IO;

using Microsoft.SharePoint.Administration;

using Aks.IO.Extensions;

namespace Aks.Deployment.SharePoint.Administration.Extensions
{
	/// <summary>
	/// Extension methods for the <see cref="SPIisSettings"/> class.
	/// </summary>
	public static class SPIisSettingsExtension
	{
		#region | Browser Capabilities |

		private const string BrowserCapsDirectory = "App_Browsers";

		#region | Copy From Feature |

		/// <summary>
		/// Copies the browser capabilities file from feature.
		/// </summary>
		/// <param name="iis">The IIS.</param>
		/// <param name="featureDirectory">The feature directory.</param>
		/// <param name="fileName">Name of the file.</param>
		/// <exception cref="UnauthorizedAccessException">The caller does not have the required permission.</exception>
		/// <exception cref="ArgumentNullException"><paramref name="iis"/>, <paramref name="featureDirectory"/>, or <paramref name="fileName"/> is <c>null</c> or empty.</exception>
		/// <exception cref="PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.</exception>
		/// <exception cref="DirectoryNotFoundException">The specified <paramref name="featureDirectory"/> does not exist in the standard SharePoint features directory.</exception>
		/// <exception cref="FileNotFoundException">The specified <paramref name="fileName"/> was not found.</exception>
		/// <exception cref="IOException">The destination directory or file is read-only or already exists.</exception>
		/// <exception cref="NotSupportedException">Either the <paramref name="featureDirectory"/> or <paramref name="fileName"/> is in an invalid format.</exception>
		public static void CopyBrowserCapsFileFromFeature(this SPIisSettings iis, string featureDirectory, string fileName)
		{
			CopyBrowserCapsFileFromFeature(iis, featureDirectory, fileName, false);
		}


		/// <summary>
		/// Copies the browser capabilities file from feature.
		/// </summary>
		/// <param name="iis">The IIS.</param>
		/// <param name="featureDirectory">The feature directory.</param>
		/// <param name="fileName">Name of the file.</param>
		/// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
		/// <exception cref="UnauthorizedAccessException">The caller does not have the required permission.</exception>
		/// <exception cref="ArgumentNullException"><paramref name="iis"/>, <paramref name="featureDirectory"/>, or <paramref name="fileName"/> is <c>null</c> or empty.</exception>
		/// <exception cref="PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.</exception>
		/// <exception cref="DirectoryNotFoundException">The specified <paramref name="featureDirectory"/> does not exist in the standard SharePoint features directory.</exception>
		/// <exception cref="FileNotFoundException">The specified <paramref name="fileName"/> was not found.</exception>
		/// <exception cref="IOException">The destination directory or file is read-only, or exists and <paramref name="overwrite"/> is <c>false</c>.</exception>
		/// <exception cref="NotSupportedException">Either the <paramref name="featureDirectory"/> or <paramref name="fileName"/> is in an invalid format.</exception>
		public static void CopyBrowserCapsFileFromFeature(this SPIisSettings iis, string featureDirectory, string fileName, bool overwrite)
		{
			// validate params
			if (iis == null)
			{
				throw new ArgumentNullException("iis", "The specified IIS settings object is null.");
			}
			if (String.IsNullOrEmpty(featureDirectory))
			{
				throw new ArgumentNullException("featureDirectory", "No feature directory was specified.");
			}
			if (String.IsNullOrEmpty(fileName))
			{
				throw new ArgumentNullException("fileName", "No file was specified.");
			}

			// if file name includes a directory, strip it out
			var dirSeparatorIndex = fileName.LastIndexOf(Path.DirectorySeparatorChar);
			if (dirSeparatorIndex > 0)
			{
				featureDirectory = PathExtension.Combine(featureDirectory, fileName.Substring(0, dirSeparatorIndex));
				fileName = fileName.Substring(dirSeparatorIndex + 1);
			}

			// if the browser compat directory doesn't exist, create it -- this should never happen
			var browserCompatPath = EnsureBrowserCompatDirectoryExists(iis.Path.FullName);

			// copy file
			File.Copy(PathExtension.Combine(SetupPath.Features, featureDirectory, fileName), 
				Path.Combine(browserCompatPath, fileName), overwrite);
		}


		/// <summary>
		/// Ensures the browser compat directory exists.
		/// </summary>
		/// <param name="siteRootPath">The root path for the IIS site.</param>
		/// <returns></returns>
		private static string EnsureBrowserCompatDirectoryExists(string siteRootPath)
		{
			var browserCompatPath = Path.Combine(siteRootPath, BrowserCapsDirectory);
			if (!Directory.Exists(browserCompatPath))
			{
				Directory.CreateDirectory(browserCompatPath);
			}
			return browserCompatPath;
		}

		#endregion


		#region | Delete |

		/// <summary>
		/// Deletes the browser compat file with the specified <paramref name="fileName"/>.
		/// </summary>
		/// <param name="iis">The IIS settings for the site to delete the file from.</param>
		/// <param name="fileName">Name of the file.</param>
		public static void DeleteBrowserCapsFile(this SPIisSettings iis, string fileName)
		{
			if (iis == null)
			{
				throw new ArgumentNullException("iis", "The specified IIS settings object is null.");
			}
			FileExtension.DeleteIfExists(PathExtension.Combine(iis.Path.FullName, BrowserCapsDirectory, fileName));
		}

		#endregion

		#endregion
	}
}